<?php
namespace Admin\Controller;
use Think\Controller;
class ContestController extends Controller {
    public function index(){
       $this->display();
    }
    public function contestlist(){
        //调用模型层获取数据
        $contestmodel=M('contest');
        //var_dump($contestmodel);
        /*$contest=$contestmodel->order('contest_id desc')->select();*/
        $contest=$contestmodel->table('contest c,admins a')->where("c.admin_id=a.admin_id")->field('c.*,a.*')->select();
        //调用视图层显示数据
        $this->contest=$contest;

        $this->display();
    }


    public function delContest($id){
        
        $contestmodel=M('contest');
        $contestinfo=$contestmodel->where("contest_id=$id")->find();
       unlink(realpath("./Public/photo/".$contestinfo["photo"]));



        if(false!==$contestmodel->where("contest_id=$id")->delete()){
            $this->success('比赛删除成功！');
        }else{
            $this->error('比赛删除失败');
        }
}
        public function editcontestlist(){
        $contest_id=$_GET['contest_id'];
        $contestModel=M('contest');//表明,conf中定义前缀
        $contest = $contestModel->where("contest_id=$contest_id")->select();

        //dump($lost);
        $this->contest=$contest;//name属性对应的值 $this->对应值=$find;
        $this->display();
    }

   public function updatecontest(){

      $data=$_POST;
      $file=$_FILES;
      //dump($file);
      $contest_id=$data['contest_id'];
      //dump($id);    
      $contestModel = M('contest'); // 实例化User模型
      // 要修改的数据对象属性赋值
      //$userModel->create();   // 根据表单提交的POST数据创建数据对象
      //dump($data);
    //$result=$userModel->where("id=$id")->save($data);

    if ($file['url']['size']==0) {
            //没有修改头像
            $result=$contestModel->where("contest_id=$contest_id")->save($data);
  
        }else{
            //修改头像
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize  = 3145728 ;// 设置附件上传大小
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->rootPath  =  './';
            $upload->savePath =  'Public/Uploads/adphoto/';// 设置附件上传目录
            $upload->autoSub = false;
            $upload->saveName = date("YmdHis").rand(100,999);//定义名字
            $info = $upload->upload();
    
            if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
             }else{// 上传成功 获取上传文件信息
         //保存表单数据 包括附件数据
                $data['url']= $info['url']['savename']; 
                $result=$contestModel->where("contest_id=$contest_id")->save($data);
                  if ($data['oldpic']!='nopic.jpg') {
                  $oldpicPath='Public/Admin/photo/'.$data['oldpic'];
                  unlink($oldpicPath);
                    }
                }
          }
    if($result){
           $this->success('修改成功！','../contest/contestlist');
         }
          else{
            $this->error('修改失败！');
            }
  }

    
   

public function addcontestlist(){
    $this->display();
  }
   Public function doaddcontest(){
        $data=$_POST;
        $file=$_FILES;
        //dump($file['photo']['size']);
        
        //没有头像
        $contestModel = M('contest'); // 实例化contest模型
        $createresult=$contestModel->create();   // 根据表单提交的POST数据创建数据对象
          if ($createresult) {
          $contestModel->createtime =  date("Y-m-d H:i:s");
             $contestModel->contest_name = trim(I('post.contest_name'));
             $contestModel->admin_id = trim(I('post.admin_id'));
                $contestModel->contest_intro = trim(I('post.contest_intro'));
                $contestModel->contest_pic= trim(I('post.contest_pic'));
                $contestModel->contest_phone = trim(I('post.contest_phone'));  
                /*$contestModel->contest_regist_time=date("Y-m-d H:i:s");*/
                $contestModel->createtime =  date("Y-m-d H:i:s");
               
                 //$contestModel->contest_classfly== trim(I('post.contest_classfly'));

                // $contestModel->createtime =  date("Y-m-d H:i:s"); // 设置用户的创建时间


             if ($file['contest_pic']['size']==0) {//没有上传轮播图片
                        $contestModel->photo= "1.jpg"; // 用户不上传头像默认nopic.jpg
                    }else{//有图片
                            $info=$this->MyUpload();
                            $contestModel->photo = $info['contest_pic']['savename']; // 保存上传的照片根据需要自行组装
                          }
                 $addresult=$contestModel->add(); // 写入用户数据到数据库
                     if (false !== $addresult)
                             {
                            $this->success('添加成功！', '../Contest/contestlist');
                             }else{ 
                                     $this->error('数据错误'); 
                                   }

        }else{
                $this->error($contestModel->getError());  
              }

    
    
    }

 
 public function MyUpload(){
                //上传头像
              $upload = new \Think\Upload();// 实例化上传类
              $upload->maxSize  = 3145728 ;// 设置附件上传大小
              $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
              $upload->rootPath  =  './';
              $upload->savePath =  'Public/Admin/photo/';// 设置附件上传目录
              $upload->autoSub = false;
              $upload->saveName = date("YmdHis").rand(100,999);//定义名字
               // 上传单个文件 
              $info = $upload->upload();                    
                  if(!$info) {// 上传错误提示错误信息
                      $this->error($upload->getError());
                    }else{// 上传成功 获取上传文件信息
                return $info;
                         }
    }       
 
 

}